<template>
  <el-card class="good-container">
    <template #header>
      <div class="header">
        <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd">新增咨询</el-button>
      </div>
    </template>
    <el-table
      v-loading="loading"
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="id"
        label="咨询编号"
        width="120"
      >
      </el-table-column>
      <el-table-column
        prop="coursename"
        label="咨询主题"
      >
      </el-table-column>
      <el-table-column
        label="咨询背景图"
        width="150px"
      >
        <template #default="scope">
          <img style="width: 100px; height: 100px;" :key="scope.row.id" :src="$filters.prefix(scope.row.courseimage)" alt="课程主图">
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="课程副图"
        width="150px"
      >
        <template #default="scope">
          <img style="width: 100px; height: 100px;" :key="scope.row.id" :src="$filters.prefix(scope.row.coursesimg)" alt="课程副图">
        </template>
      </el-table-column> -->
      <!-- <el-table-column
        prop="coursecopywriting"
        label="咨询文案"
      >
      </el-table-column>
      <el-table-column
        prop="courseharvest"
        label="咨询收获"
      >
      </el-table-column>
      <el-table-column
        prop="courseinformation"
        label="报名信息"
      >
      </el-table-column>
      <el-table-column
        prop="attentionevent"
        label="注意事件"
      >
      </el-table-column> -->
    
      <el-table-column
        prop="courseprice"
        label="课程售价"
      >
      </el-table-column>
      <el-table-column
       prop="coursetype"
        label="分类0团队咨询，1心理沙龙"
      >
      </el-table-column>
      <el-table-column
      prop="teacherarea"
        label="咨询所在地"
      >
        
      </el-table-column>
      <el-table-column
        label="操作"
        width="200"
      >
        <template #default="scope">
          <a style="cursor: pointer; margin-right: 10px" @click="handleEdit(scope.row.id)">修改</a>
         
          <a style="cursor: pointer; margin-right: 10px"  @click="handleDelete(scope.row.id)">删除</a>
        
        </template>
      </el-table-column>
    </el-table>
    <!--总数超过一页，再展示分页器-->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="pageSize"
      :current-page="currentPage"
      @current-change="changePage"
    />
  </el-card>
</template>

<script>
import { onMounted, reactive, ref, toRefs } from 'vue'
import axios from '@/utils/axios'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
export default {
  name: 'consultation',
  setup() {
    const multipleTable = ref(null)
    const router = useRouter()
    const state = reactive({
      loading: false,
      tableData: [], // 数据列表
      multipleSelection: [], // 选中项
      total: 0, // 总条数
      currentPage: 1, // 当前页
      pageSize: 10 // 分页大小
    })
    onMounted(() => {
      getGoodList()
    })
    // 获取轮播图列表
    const getGoodList = () => {
      state.loading = true
      axios.get('/consultation/consultationList', {
        params: {
          pageNumber: state.currentPage,
          pageSize: state.pageSize
        }
      }).then(res => {
        state.tableData = res.list
        state.total = res.totalCount
        state.currentPage = res.currPage
        state.loading = false
      })
    }
    const handleAdd = () => {
      router.push({ path: '/addconsultation' })
    }
    const handleEdit = (id) => {
      router.push({ path: '/addconsultation', query: { id } })
    }
    // 选择项
    const handleSelectionChange = (val) => {
      state.multipleSelection = val
    }
    const changePage = (val) => {
      state.currentPage = val
      getGoodList()
    }
    const handleStatus = (id, status) => {
      axios.put(`/course/status/${status}`, {
        ids: id ? [id] : []
      }).then(() => {
        ElMessage.success('修改成功')
        getGoodList()
      })
    }
    const handleDelete = (id) => {
      console.log(id);
      axios.delete(`/consultation`, {
         data: {
          ids: id ? [id] : []
        }
      }).then((res) => {
        console.log(res);
        ElMessage.success('删除成功')
        getGoodList()
      })
    }
    return {
      ...toRefs(state),
      multipleTable,
      handleSelectionChange,
      handleAdd,
      handleEdit,
      getGoodList,
      changePage,
      handleStatus,
      handleDelete
    }
  }
}
</script>

<style scoped>
  .good-container {
    min-height: 100%;
  }
  .el-card.is-always-shadow {
    min-height: 100%!important;
  }
</style>